package com.kun.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kun.entity.Book;
import com.kun.entity.BookExample;
import com.kun.entity.BookExample.Criteria;
import com.kun.mapper.cluster.custom.BookMapperRCustom;
import com.kun.mapper.master.custom.BookMapperWCustom;
import com.kun.service.BookService;
import com.kun.util.PaginationUtil;


/**
 * @description:
 * @author: KM
 * 2018年3月27日
 */
@Service
public class BookServiceImpl implements BookService {
	
	@Autowired
	private BookMapperWCustom masterBookMapper;
	
	@Autowired
	private BookMapperRCustom bookMapperRCustom;
	
	public Book selectBookById(int bookid){
		return bookMapperRCustom.selectByPrimaryKey(bookid);
	}
	
	/**
	 * 分页条件查询
	 * */
	@SuppressWarnings("rawtypes")
	public Map<String,Object> selectAllBook(int begin,int length,String jsonString,boolean isShow){
		BookExample bex = new BookExample();
		Criteria criteria = bex.createCriteria();
		if(isShow==false){
			criteria.andIsShowEqualTo("1");
		}
		String order = "";
		//name=红楼梦
		if(!"".equals(jsonString)&&jsonString!=null&&jsonString.length()>0){
			JSONObject json = JSONObject.parseObject(jsonString);
			Map cMap = (Map)json;
			if (cMap != null && !cMap.isEmpty()) {
				Iterator it = cMap.keySet().iterator();
				while(it.hasNext()){
					String name = (String)it.next();
					try {
						if(!cMap.get(name).equals(null)){
							if(name.equals("type")){//类型查询
								criteria.andTypeidLike(cMap.get(name) + "%");
							}else if(name.equals("author")){//作者查询
								criteria.andBookauthorLike("%"+ java.net.URLDecoder.decode((String)cMap.get(name),"UTF-8").trim() +"%");
							}else if(name.equals("number")){//编号查询
								criteria.andBooknumberLike("%"+ java.net.URLDecoder.decode((String)cMap.get(name),"UTF-8").trim() +"%");
							}else if(name.equals("press")){
								criteria.andBookpressLike("%"+ java.net.URLDecoder.decode((String)cMap.get(name),"UTF-8").trim() +"%");	
							}else if(name.equals("name")){
								criteria.andBooknameLike("%"+ java.net.URLDecoder.decode((String)cMap.get(name),"UTF-8").trim() +"%");	
							}else if(name.equals("isShow")){
								criteria.andIsShowEqualTo((String)cMap.get(name));
							}else if(name.equals("shelveTime")){//上架时间 排序
								order = "bookShelveTime  "+cMap.get(name);
							}else if(name.equals("borrow")){//借出记录数量 排序 
								if(!"".equals(order)){
									order+=",bookBorrow  "+cMap.get(name);
								}else{
									order = "bookBorrow  "+cMap.get(name);
								}
							}
						}
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
			}
		}
		if(!"".equals(order)){
			bex.setOrderByClause(order);
		}
		int allRecord = bookMapperRCustom.countByExample(bex);
		PaginationUtil pagination = new PaginationUtil((begin-1)*length,length,allRecord);
		bex.setPagination(pagination);
		List<Book> bookList = bookMapperRCustom.selectByExample(bex);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("allRecord", allRecord);
		resultMap.put("bookList", bookList);
		return resultMap;
	}
	
	public int insertReturnPrimaryKey(Book book){
		book.setBookborrow(0);
		book.setBookshelvetime(new Date());
		book.setIsShow("1");
		return masterBookMapper.insertReturnPrimaryKey(book);
	}
	
	public int updateBook(Book book){
		return masterBookMapper.updateByPrimaryKeySelective(book);
	}
	
	public int deleteBook(int bookId){
		return masterBookMapper.deleteByPrimaryKey(bookId);
	}
	
	public List<Integer> selectBookLikeBookName(String bookname,String typeid){
		if(bookname==null){
			bookname="";
		}
		if(typeid==null){
			typeid="";
		}
		
		if("".equals(bookname)&&"".equals(typeid)){
			return null;//不输入直接返回null
		}else{
			List<Integer> l = new ArrayList<Integer>();
			l = bookMapperRCustom.selectPrimaryKeyLikeBookName(bookname,typeid);
			return l;
		}
	}
	
	/**
	 * 根据booknumber增减库存
	 * */
	public int updateAmountByBookNumber(String bookNumber,int amount,boolean isAddBorrow){
		int borrowAmount = 0;
		if(isAddBorrow==true&&amount<0){//+借阅记录
			borrowAmount = -amount;
		}
		return masterBookMapper.updateBookAmountByBooknumber(bookNumber, amount,borrowAmount);
	}

	public Book selectBookByBooknumber(String bookNumber){
		BookExample bex = new BookExample();
		Criteria criteria = bex.createCriteria();
		criteria.andBooknumberEqualTo(bookNumber);
		List<Book>  lb = bookMapperRCustom.selectByExample(bex);
		if(lb.size()>0){
			return lb.get(0);
		}
		return null;
	}

	@Override
	public int updateAddBookOwnerByBooknumber(String bookNumber, String ownerName) {
		ownerName = "," + ownerName;
		return masterBookMapper.updateAddBookOwnerByBooknumber(bookNumber, ownerName);
	}

	@Override
	public int updateDeleteBookOwnerByBooknumber(String bookNumber, String ownerName) {
		ownerName = "," + ownerName;
		return masterBookMapper.updateDeleteBookOwnerByBooknumber(bookNumber, ownerName);
	}
}
